//package cn.nfwx.tv.controller;
//
//import android.app.Activity;
//import android.content.Context;
//import android.media.AudioManager;
//import android.view.GestureDetector;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.ViewTreeObserver;
//import android.view.WindowManager;
//import android.widget.ImageView;
//import android.widget.RelativeLayout;
//import android.widget.TextView;
//
//import java.lang.ref.WeakReference;
//import java.util.ArrayList;
//import java.util.List;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.util.DensityUtil;
//import cn.nfwx.tv.widget.ijkplayer.IjkVideoView;
//
///**
// * Created by dell on 2018/3/26.
// */
//
//public class IjkPlayerViewController implements GestureDetector.OnGestureListener, View.OnTouchListener {
//
//    private static IjkPlayerViewController instance;
//    private WeakReference<Activity> weakReference;
//
//    private IjkVideoView _player;
//    private GestureDetector gestureDetector;
//    private RelativeLayout gesture_volume_layout, gesture_bright_layout, gesture_progress_layout;
//    private ImageView gesture_iv_player_volume, gesture_iv_player_bright, gesture_iv_player_progress;
//    private TextView geture_tv_volume_percentage, geture_tv_bright_percentage, geture_tv_progress_percentage;
//
//    private AudioManager audiomanager;
//    private int maxVolume, currentVolume;
//    private int playerWidth, playerHeight;
//    private int GESTURE_FLAG = 0;// 1,调节进度，2，调节音量,3.调节亮度
//    private int LAST_GESTURE_FLAG = 0;
//    private float lastOldY;
//    private float mBrightness = -1f; // 亮度
//    private static final float STEP_PROGRESS = 2f;// 设定进度滑动时的步长，避免每次滑动都改变，导致改变过快
//    private static final float STEP_VOLUME = 2f;// 协调音量滑动时的步长，避免每次滑动都改变，导致改变过快
//    private boolean firstScroll = false;
//    private static final int GESTURE_MODIFY_PROGRESS = 1;
//    private static final int GESTURE_MODIFY_VOLUME = 2;
//    private static final int GESTURE_MODIFY_BRIGHT = 3;
//
//    private List<PlayerViewControlListener> playerViewControlListeners = new ArrayList<>();
//    private int playingTime;
//    // 降低切换视频的灵敏度
//    private float x1;
//    //    private GrabP2PSPlayerBridge mP2PSPlayerBridge;
//    private boolean enable = true;
//
//    public static IjkPlayerViewController getInstance() {
//        if (instance == null) {
//            instance = new IjkPlayerViewController();
//        }
//        return instance;
//    }
//
//    public void addPlayerViewControlListener(PlayerViewControlListener playerViewControlListener) {
//        playerViewControlListeners.add(playerViewControlListener);
//    }
//
//    public void removePlayerViewControlListener(PlayerViewControlListener playerViewControlListener) {
//        playerViewControlListeners.remove(playerViewControlListener);
//    }
//
////    public void attachPlayerBridge(GrabP2PSPlayerBridge p2PSPlayerBridge) {
////        mP2PSPlayerBridge = p2PSPlayerBridge;
////    }
//
////    @Override
////    public void onSeekComplete() {
////        seekComplete();
////    }
//
//    public void setEnable(boolean enable) {
//        this.enable = enable;
//    }
//
//    public interface PlayerViewControlListener {
//        // 滑动时移
//        void progressControl(int playTime);
//
//        // 滑动完成时移
//        void progressControlFinish(int playTime);
//
//        // 上一个
//        void playBack();
//
//        // 下一个
//        void playNext();
//    }
//
//    public IjkPlayerViewController() {
//    }
//
//    private <T extends View> T findView(View view, int rid) {
//        return view.findViewById(rid);
//    }
//
//    private <T extends View> T findView(int rid) {
//        return weakReference.get().findViewById(rid);
//    }
//
//    public void attachPlayer(Activity activity, IjkVideoView player) {
//        weakReference = new WeakReference<Activity>(activity);
//        enable = true;
//
//        _player = player;
//
//        gesture_volume_layout = findView(player, R.id.gesture_volume_layout);
//        gesture_bright_layout = findView(player, R.id.gesture_bright_layout);
//        gesture_progress_layout = findView(player, R.id.gesture_progress_layout);
//
//        gesture_iv_player_volume = findView(player, R.id.gesture_iv_player_volume);
//        gesture_iv_player_bright = findView(player, R.id.gesture_iv_player_bright);
//        gesture_iv_player_progress = findView(player, R.id.gesture_iv_player_progress);
//
//        geture_tv_volume_percentage = findView(player, R.id.geture_tv_volume_percentage);
//        geture_tv_bright_percentage = findView(player, R.id.geture_tv_bright_percentage);
//        geture_tv_progress_percentage = findView(player, R.id.geture_tv_progress_percentage);
//
//        gestureDetector = new GestureDetector(weakReference.get(), this);
//        gestureDetector.setIsLongpressEnabled(false);
//
//        audiomanager = (AudioManager) weakReference.get().getSystemService(Context.AUDIO_SERVICE);
//        maxVolume = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量
//        currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
//
//        _player.setLongClickable(true);
//        _player.setOnTouchListener(this);
//        _player.setClickable(true);
//
//        /** 获取视频播放窗口的尺寸 */
//        ViewTreeObserver viewObserver = _player.getViewTreeObserver();
//        viewObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                if (_player != null) {
//                    _player.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                    playerWidth = _player.getWidth();
//                    playerHeight = _player.getHeight();
//                }
//            }
//        });
//    }
//
//    /**
//     * 清除
//     */
//    public void release() {
//        playStop();
//        if (_player != null) {
//            _player.release();
//            _player = null;
//        }
//
//        instance = null;
//    }
//
//    /**
//     * 转换其他player
//     */
//    public void stop() {
//        playStop();
//        instance = null;
//    }
//
//    @Override
//    public boolean onDown(MotionEvent e) {
//        firstScroll = true;// 设定是触摸屏幕后第一次scroll的标志
//        return false;
//    }
//
//
//    @Override
//    public void onShowPress(MotionEvent e) {
//
//    }
//
//    @Override
//    public boolean onSingleTapUp(MotionEvent e) {
//        return false;
//    }
//
//    @Override
//    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//        if (e1 == null || e2 == null || !enable) {
//            return true;
//        }
//        float mOldX = e1.getX(), mOldY = e1.getY();
//        //LogUtil.e("ttt", "mOldX--" + mOldX + "--mOldY--" + mOldY + "--mNewX--" + mNewX + "--mNewY--" + mNewY);
//        int y = (int) e2.getRawY();
//        if (firstScroll) {// 以触摸屏幕后第一次滑动为标准，避免在屏幕上操作切换混乱
//            // 横向的距离变化大则调整进度，纵向的变化大则调整音量
//            if (Math.abs(distanceX) >= Math.abs(distanceY)) {
//                //gesture_progress_layout.setVisibility(View.VISIBLE);
//                gesture_volume_layout.setVisibility(View.GONE);
//                gesture_bright_layout.setVisibility(View.GONE);
//                GESTURE_FLAG = GESTURE_MODIFY_PROGRESS;
//            } else {
//                if (mOldX > playerWidth * 3.0 / 5) {// 音量
//                    gesture_progress_layout.setVisibility(View.GONE);
//                    gesture_volume_layout.setVisibility(View.VISIBLE);
//                    gesture_bright_layout.setVisibility(View.GONE);
//                    GESTURE_FLAG = GESTURE_MODIFY_VOLUME;
//                } else if (mOldX < playerWidth * 2.0 / 5) {// 亮度
//                    gesture_progress_layout.setVisibility(View.GONE);
//                    gesture_bright_layout.setVisibility(View.VISIBLE);
//                    gesture_volume_layout.setVisibility(View.GONE);
//                    GESTURE_FLAG = GESTURE_MODIFY_BRIGHT;
//                }
//            }
//        }
//        // 如果每次触摸屏幕后第一次scroll是调节进度，那之后的scroll事件都处理音量进度，直到离开屏幕执行下一次操作
//        if (GESTURE_FLAG == GESTURE_MODIFY_PROGRESS) {
//            // distanceX=lastScrollPositionX-currentScrollPositionX，因此为正时是快进
//            if (Math.abs(distanceX) > Math.abs(distanceY)) {// 横向移动大于纵向移动
//                if (distanceX >= DensityUtil.dip2px(NFWXApplication.getInstance(), STEP_PROGRESS)) {// 快退，用步长控制改变速度，可微调  
//                    gesture_iv_player_progress.setImageResource(R.mipmap.progress_back);
//                    playingTime -= 3;// scroll方法执行一次快退3秒
//                } else if (distanceX <= -DensityUtil.dip2px(NFWXApplication.getInstance(), STEP_PROGRESS)) {// 快进
//                    gesture_iv_player_progress.setImageResource(R.mipmap.progress_next);
//                    playingTime += 3;// scroll执行一次快进3秒
//                }
//                for (PlayerViewControlListener playerViewControlListener : playerViewControlListeners) {
//                    playerViewControlListener.progressControl(playingTime);
//                }
//            }
//        }
//        // 如果每次触摸屏幕后第一次scroll是调节音量，那之后的scroll事件都处理音量调节，直到离开屏幕执行下一次操作
//        else if (GESTURE_FLAG == GESTURE_MODIFY_VOLUME) {
//            currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
//            if (Math.abs(distanceY) > Math.abs(distanceX)) {// 纵向移动大于横向移动
//                if (distanceY >= DensityUtil.dip2px(NFWXApplication.getInstance(), STEP_VOLUME)) {// 音量调大,注意横屏时的坐标体系,尽管左上角是原点，但横向向上滑动时distanceY为正
//                    if (currentVolume < maxVolume) {// 为避免调节过快，distanceY应大于一个设定值
//                        currentVolume++;
//                    }
//                    gesture_iv_player_volume.setSelected(false);
//                } else if (distanceY <= -DensityUtil.dip2px(NFWXApplication.getInstance(), STEP_VOLUME)) {// 音量调小
//                    if (currentVolume > 0) {
//                        currentVolume--;
//                        if (currentVolume == 0) {// 静音，设定静音独有的图片
//                            gesture_iv_player_volume.setSelected(true);
//                        }
//                    }
//                }
//                int percentage = (currentVolume * 100) / maxVolume;
//                geture_tv_volume_percentage.setText(percentage + "%");
//                audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
//            }
//        }
//        // 如果每次触摸屏幕后第一次scroll是调节亮度，那之后的scroll事件都处理亮度调节，直到离开屏幕执行下一次操作
//        else if (GESTURE_FLAG == GESTURE_MODIFY_BRIGHT) {
//            if (weakReference == null || weakReference.get() == null) {
//                return false;
//            }
//            gesture_iv_player_bright.setImageResource(R.mipmap.souhu_player_bright);
//            if (lastOldY != mOldY) {
//                lastOldY = mOldY;
//                mBrightness = weakReference.get().getWindow().getAttributes().screenBrightness;
//            }
//            if (mBrightness < 0) {
//                if (mBrightness <= 0.00f)
//                    mBrightness = 0.50f;
//                if (mBrightness < 0.01f)
//                    mBrightness = 0.01f;
//            }
//            if (weakReference.get() != null) {
//                WindowManager.LayoutParams lpa = weakReference.get().getWindow().getAttributes();
//                lpa.screenBrightness = mBrightness + (mOldY - y) / playerHeight;
//                if (lpa.screenBrightness > 1.0f)
//                    lpa.screenBrightness = 1.0f;
//                else if (lpa.screenBrightness < 0.01f)
//                    lpa.screenBrightness = 0.01f;
//                weakReference.get().getWindow().setAttributes(lpa);
//                geture_tv_bright_percentage.setText((int) (lpa.screenBrightness * 100) + "%");
//            }
//        }
//
//        firstScroll = false;// 第一次scroll执行完成，修改标志
//        return true;
//    }
//
//    @Override
//    public void onLongPress(MotionEvent e) {
//
//    }
//
//    @Override
//    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//        return true;
//    }
//
//    @Override
//    public boolean onTouch(View v, MotionEvent event) {
//        if (!enable) {
//            return gestureDetector.onTouchEvent(event);
//        }
//        // 手势里除了singleTapUp，没有其他检测up的方法
//        if (event.getAction() == MotionEvent.ACTION_DOWN) {
//            x1 = event.getRawX();
//        } else if (event.getAction() == MotionEvent.ACTION_UP) {
//            LAST_GESTURE_FLAG = GESTURE_FLAG;
//            // 时移
//            if (LAST_GESTURE_FLAG == GESTURE_MODIFY_PROGRESS) {
//                for (PlayerViewControlListener playerViewControlListener : playerViewControlListeners) {
//                    playerViewControlListener.progressControlFinish(playingTime);
//                }
//            }
//            // 上一个下一个
//            if (LAST_GESTURE_FLAG == GESTURE_MODIFY_PROGRESS) {
//                float d = event.getRawX() - x1;
//                if (Math.abs(d) > 150) {
//                    if (playingTime > 0) {
//                        for (PlayerViewControlListener playerViewControlListener : playerViewControlListeners) {
//                            playerViewControlListener.playBack();
//                        }
//                    } else if (playingTime < 0) {
//                        for (PlayerViewControlListener playerViewControlListener : playerViewControlListeners) {
//                            playerViewControlListener.playNext();
//                        }
//                    }
//                }
//            }
//            playingTime = 0;
//            GESTURE_FLAG = 0;// 手指离开屏幕后，重置调节音量或进度的标志
//            gesture_progress_layout.setVisibility(View.GONE);
//            gesture_volume_layout.setVisibility(View.GONE);
//            gesture_bright_layout.setVisibility(View.GONE);
//            if (LAST_GESTURE_FLAG != 0) {
//                LAST_GESTURE_FLAG = 0;
//                return true;
//            } else {
//                return gestureDetector.onTouchEvent(event);
//            }
//        }
//        return gestureDetector.onTouchEvent(event);
//    }
//
//
//    /**
//     * 告知统计会话执行了seek
//     */
//    public void seek() {
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.seek((int) _player.getCurrentPosition(), _player.getDuration());
////        }
//    }
//
//    /**
//     * 告知统计会话seek完成
//     */
//    public void seekComplete() {
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.seekComplete();
////        }
//    }
//
//    /**
//     * 统一处理暂停
//     */
//    public void playPause() {
//        if (_player != null) {
//            // 暂停
//            _player.pause();
//        }
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.pause();
////        }
//    }
//
//    /**
//     * 手动暂停
//     */
//    public void handPause() {
//        if (_player != null) {
//            // 暂停
//            _player.handpause();
//        }
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.pause();
////        }
//    }
//
//    /**
//     * 统一处理恢复
//     */
//    public void playResume() {
//        if (_player != null) {
//            _player.start();
//            // 手动隐藏广告
//            //findView(R.id.iv_advertise).setVisibility(View.GONE);
//        }
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.resume();
////        }
//    }
//
//    public void restart(String lastPath) {
//        if (_player != null) {
//            _player.restart(lastPath);
//
//            // 手动隐藏广告
//            //findView(R.id.iv_advertise).setVisibility(View.GONE);
//        }
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.resume();
////        }
//    }
//
//    /**
//     * 统一关闭
//     */
//    public void playStop() {
//        if (_player != null) {
//            _player.stop();
//        }
////        if (mP2PSPlayerBridge != null) {
////            mP2PSPlayerBridge.stop();
////        }
//    }
//
//}
//
